package com.saifan.wyy_ov.ui.album.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.album.model.ImageGroup;
import com.saifan.wyy_ov.utils.CropSquareTransformation;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


/**
 * @ClassName: PhotoShowAdapter
 * @Description: 照片选择适配器
 * @author  WangLi
 * @data:  2014年12月5日 上午12:43:26
 * @version:  V1.0
 */
public class PhotoShowAdapter extends BaseAdapter {
    private Context context;
    public ArrayList<String> selectPathList = null;
    public ImageGroup imgGroup;
    private GridView gridView;

    public PhotoShowAdapter(Context context, ImageGroup img, ArrayList<String> selectPathList, GridView grid) {
        this.context = context;
        this.selectPathList = selectPathList;
        this.imgGroup = img;
        this.gridView = grid;
    }

    @Override
    public int getCount() {
        return imgGroup.getImageCount();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.photoshow_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.photo_wall_item_photo);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.photo_wall_item_cb);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String path = imgGroup.getImages().get(position);
        if (holder.imageView.getTag() == null || !holder.imageView.getTag().equals(path)) {
            ImgUtil.DisPlayImgForLocal(path, holder.imageView, context);
            holder.imageView.setTag(path);
        }
        if(selectPathList.contains(path)){
            holder.checkBox.setChecked(true);
            holder.imageView.setColorFilter(context.getResources().getColor(R.color.image_checked_bg));
        }else{
            holder.checkBox.setChecked(false);
            holder.imageView.setColorFilter(null);
        }
        return convertView;
    }

    /**
     * 刷新view
     * @param index
     */
    public void refreshView(int index){
        int visiblePos = gridView.getFirstVisiblePosition();
        View view = gridView.getChildAt(index-visiblePos);
        ViewHolder holder = (ViewHolder)view.getTag();

        if(holder.checkBox.isChecked()){
            holder.checkBox.setChecked(false);
            holder.imageView.setColorFilter(null);
        }else{
            holder.checkBox.setChecked(true);
            holder.imageView.setColorFilter(context.getResources().getColor(R.color.image_checked_bg));
        }
    }

    class ViewHolder {
        ImageView imageView;
        CheckBox checkBox;
    }
}
